package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.SearcheObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.impl.StrategyDetailServiceImpl;
import cn.wolfcode.trip.base.service.impl.TravelCommendServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("searchs")
public class SearchController {

    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelCommendMapper travelCommendMapper;
    @Autowired
    private ITravelCommendService travelCommendService;

    /**
     * 查询攻略和游记的数量
     * @param qo
     * @param
     * @return
     */
    @GetMapping("/count")
    @ResponseBody
    public Map getByCount(SearcheObject qo){
        HashMap<String, Object> count = new HashMap<>();
        int strategyCount = strategyDetailMapper.selectForCount(qo);
        int travelCount = travelCommendMapper.selectForCount(qo);
        count.put("strategyCount",strategyCount);
        count.put("travelCount",travelCount);
        /*PageInfo strateDetailPageInfo = strategyDetailService.query(qo);
        PageInfo travelCommendPageInfo = travelCommendService.queryForSearchList(qo);
        if (strateDetailPageInfo!=null){
            count.put("strateDetailPageInfo",strateDetailPageInfo);
            return count;
        }else if (travelCommendPageInfo!=null){
            count.put("travelCommendPageInfo",travelCommendPageInfo);
            return count;
        }*/
        return count;
    }

    /**q
     * 通过关键字和type查询相对应的对象
     */
    @GetMapping
    @ResponseBody
    public PageInfo query(SearcheObject qo){
        if (qo.getType() == 1){
            qo.setPageSize(4);
            PageInfo strategyDetailPageInfo = strategyDetailService.query(qo);
            return strategyDetailPageInfo;
        }else if (qo.getType() == 2){
            qo.setPageSize(4);
            return travelCommendService.queryForSearchList(qo);
        }
       return null;
    }


}
